@extends('frontend._layouts.base')

<?
use \App\Models\Product;
use \App\Models\ProductCategory;
?>

<? if(count($params) == 0):?>
	@section('content')
		<? foreach( $page->rows()->get() as $row):?>
			<div class="row content-row">
				<? foreach($row->blocks()->get() as $block):?>
					<div class="col-lg-<?=$block->span?> col-md-<?=$block->span?> col-sm-<?=$block->span?> col-lg-offset-<?=$block->offset?> col-md-offset-<?=$block->offset?> col-sm-offset-<?=$block->offset?> content-block"><?=$block->content?></div>
				<? endforeach;?>
			</div>
		<? endforeach;?>
	@stop
<? endif;?>

@section("products")
	<? if(count($params) == 1):?>
		<? //var_dump($params)?>
		<?
			$category = ProductCategory::where("slug", $params[0])->firstOrFail();
			$products = array_chunk($category->products()->where("status",1)->get()->all(), 4) ;
		?>

			@section("pagetitle")
 - {{ $category->name }}
@endsection
		<h1>{{ $category->name }} Products</h1>
		{{ $category->description }}
		@foreach ($products as $productrow)
			<div class="row">
				@foreach ($productrow as $product)
					<?
						//$int = $product->images()->whereRaw("typeid in (1,2)")->orderBy("typeid","ASC")->firstOrFail()->get();
						//var_dump($int);
						try {
							$productimage = $product->images()->whereRaw("typeid in (1,2)")->firstOrFail()->imagefilename;
							//var_dump($productimage);
						} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
							$productimage = "";
						}

					?>
					<div class="col-lg-3 col-md-3 col-sm-3">
						<div class="product-grid-item">
							<a href="/products/{{$params[0]}}/{{$product->slug}}">
								<img src="<?=Croppa::url((($productimage == "")?"/images/misc/missing-image.png":"/images/products/{$product->id}/{$productimage}"), 400, 500)?>" class="img-responsive">
								<div class="productinfo">
									<div class="title">{{ $product->name }}</div>
									<div class="shortdesc">
										<hr>
										<p>{{ strip_tags($product->shortdescription) }}</p>
										<? if(trim($product->packaging()) != ""):?>
											<p><strong>Packaging</strong><br>{{ strip_tags($product->packaging()) }}<p>
										<? endif;?>	
									</div>
									
								</div>
							</a>
						</div>
					</div>
				@endforeach
			</div>
		@endforeach
	<? endif;?>
@stop

<? if(count($params) > 1):?>
@section("product")
	<? //var_dump($params)?>
	<?
		$category = ProductCategory::where("slug", $params[0])->firstOrFail();
		$product = Product::where("slug", $params[1])->firstOrFail();
		$attributes = $product->attributes()->get();
		$relatedProducts = $product->relatedProducts()->get();
		$files = $product->files()->get();
	?>

	@section("pagetitle")
 - {{ $product->name }}
@endsection
	
	@section("pagedescription")
{{strip_tags($product->shortdescription)}}
@endsection

	<?
		try {
			$productimage = $product->images()->whereRaw("typeid in (1,2)")->firstOrFail()->imagefilename;
		} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
			$productimage = "";
		}
	?>

	<h2 class="product-title">{{ $product->name }} <small>{{-- {{ strip_tags($product->shortdescription) }} --}}</small></h2>

	<div class="row">
		<div class="col-lg-6 col-md-4 col-sm-4">
			<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2">
				<img src="<?=Croppa::url((($productimage == "")?"/images/misc/missing-image.png":"/images/products/{$product->id}/{$productimage}"), 400)?>" class="img-responsive">
			</div>
			<div class="col-lg-12 col-md-12 col-sm-12">
				@if(count($relatedProducts) > 0)
				<div class="product-related-products">
					<h4>Related Products</h4>
					<hr>
					<div class="row">
						@foreach($relatedProducts as $relatedProduct)
							<?
								try {
									$relatedproductimage = $relatedProduct->images()->whereRaw("typeid in (1,2)")->firstOrFail()->imagefilename;
								} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
									$relatedproductimage = "";
								}
							?>
							<div class="col-lg-4 col-md-4 col-sm-4">
								<div class="product-grid-item small">
									<a href="/products/{{$relatedProduct->categories()->firstOrFail()->slug}}/{{$relatedProduct->slug}}">
										<img src="<?=Croppa::url((($relatedproductimage == "")?"/images/misc/missing-image.png":"/images/products/{$relatedProduct->id}/{$relatedproductimage}"), 400)?>" class="img-responsive">
										<div class="productinfo">
											<div class="title">{{ $relatedProduct->name }}</div>
											{{-- <div class="shortdesc">
												{{ strip_tags($relatedProduct->shortdescription) }}
											</div> --}}
											
										</div>
									</a>
								</div>
							</div>
							
						@endforeach
					</div>
				</div>
				@endif
			</div>
		</div>
		<div class="col-lg-6 col-md-8 col-sm-8 product-details">
			<!-- Nav tabs -->
			<ul class="nav nav-tabs"> <!-- grey colour from navbar -->
				<li class="active"><a href="#description" data-toggle="tab">Description</a></li>
				@foreach($attributes as $attribute)
					<li class=""><a href="#attr_{{$attribute->id}}" data-toggle="tab">{{$attribute->attributename}}</a></li>
				@endforeach
				@if(count($files) > 0)
					<li class=""><a href="#downloads" data-toggle="tab">Downloads</a></li>
				@endif
			</ul>

			<!-- Tab panes -->
			<div class="tab-content">
				<div class="tab-pane active" id="description">{{ str_replace("&nbsp;"," ", $product->description) }}</div>
				@foreach($attributes as $attribute)
					<?/*<li class=""><a href="#attr_{{$attribute->id}}">{{$attribute->attributename}}</a></li>*/?>
					<div class="tab-pane" id="attr_{{$attribute->id}}">{{ str_replace("&nbsp;"," ", $attribute->pivot->content) }}</div>
				@endforeach
				@if(count($files) > 0)
					<div class="tab-pane" id="downloads">
						<h3>File Downloads</h3>
						<ul>
							@foreach($files as $file)
							<li>{{$file->filetype()}}: <a href="/files/products/{{$product->id}}/{{$file->filename}}" target="_blank">{{$file->filename}}</a></li>
							@endforeach
						</ul>
					</div>
				@endif
			</div>

		</div>

	</div>
@stop
<? endif;?>